package org.gp953.pet.controller;

import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.util.AjaxResult;
import org.gp953.basic.util.LoginContext;
import org.gp953.basic.util.PageList;
import org.gp953.pet.domain.Pet;
import org.gp953.pet.domain.SearchMasterMsg;
import org.gp953.pet.query.SearchMasterMsgQuery;
import org.gp953.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {

        try {
            seachMasterMsgService.publish(searchMasterMsg,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    // 查询待处理消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.pending(query, LoginContext.getLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    //查询已处理消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.processed(query, LoginContext.getLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    //处理消息
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request) {

        try {
            seachMasterMsgService.handle(pet,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }



    //个人中心，用户自己的消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.queryUserMsg(query, LoginContext.getLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }


}
